package com.imooc.sell.Service.impl;

import com.imooc.sell.DTO.CartDTO;
import com.imooc.sell.Dao.ProductCategoryDao;
import com.imooc.sell.Dao.ProductInfoDao;
import com.imooc.sell.Entity.ProductInfo;
import com.imooc.sell.Service.ProductInfoService;
import com.imooc.sell.enums.ExceptionResultEnum;
import com.imooc.sell.enums.ProductStatusEnum;
import com.imooc.sell.exception.SellException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductInfoServiceImpl implements ProductInfoService{

    @Autowired
    private ProductInfoDao repository;

    @Override
    public ProductInfo findOne(String productId) {
        return repository.findById(productId).get();
    }

    @Override
    public List<ProductInfo> findUpAll() {
        return repository.findByProductStatus( ProductStatusEnum.UP.getCode());
    }
    @Override
    public Page<ProductInfo> findAll(Pageable pageable) {
        return repository.findAll(pageable);
    }

    @Override
    public ProductInfo save(ProductInfo productInfo) {
        return repository.save(productInfo);
    }

    @Override
    @Transactional
    public void increaseStock(List<CartDTO> cartDTOS) {
        for(CartDTO cartDTO:cartDTOS){
            ProductInfo productInfo = repository.findById(cartDTO.getProductId()).get();
            if(productInfo == null){throw new SellException(ExceptionResultEnum.PRODUCT_NOT_EXIST);}
            Integer result = productInfo.getProductStock()+cartDTO.getProductQuantity();

            productInfo.setProductStock(result);
            repository.save(productInfo);
        }
    }

    @Override
    @Transactional
    public void decreaseStock(List<CartDTO> cartDTOS) {
        for(CartDTO cartDTO:cartDTOS){
            ProductInfo productInfo = repository.findById(cartDTO.getProductId()).get();
            if(productInfo == null){throw new SellException(ExceptionResultEnum.PRODUCT_NOT_EXIST);}
            Integer result = productInfo.getProductStock()-cartDTO.getProductQuantity();
            if(result < 0){throw new SellException(ExceptionResultEnum.PRODUCT_STOCK_ERROR);}

            productInfo.setProductStock(result);
            repository.save(productInfo);
        }
    }

    @Override
    public ProductInfo onSale(String productId) {
        ProductInfo productInfo = repository.findById(productId).get();
        if(productInfo == null){throw new SellException(ExceptionResultEnum.PRODUCT_NOT_EXIST);}
        if(productInfo.getProductStatus() == 0){
            throw new SellException(ExceptionResultEnum.PRODUCT_STATUS_ERROR);
        }
        productInfo.setProductStatus(0);
        return repository.save(productInfo);
    }

    @Override
    public ProductInfo offSale(String productId) {
        ProductInfo productInfo = repository.findById(productId).get();
        if(productInfo == null){throw new SellException(ExceptionResultEnum.PRODUCT_NOT_EXIST);}
        if(productInfo.getProductStatus() == 1){
            throw new SellException(ExceptionResultEnum.PRODUCT_STATUS_ERROR);
        }
        productInfo.setProductStatus(1);
        return repository.save(productInfo);
    }
}
